<?php
//初始化
require(dirname(__FILE__) . '/includes/init1.php');
require(dirname(__FILE__) . '/includes/simple_html_dom.php');
set_time_limit(0);
$group_list = $db->getAll("select group_id,link from ".$ecs->table('yhd_group')." where end_time=0");
foreach($group_list as $index=>$group){
	$group_id = $group['group_id'];
	$curl = curl_init(); 
	curl_setopt($curl, CURLOPT_URL,$group['link']); 
	curl_setopt($curl, CURLOPT_REFERER, 'http://www.yihaodian.com'); 
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
	$result = curl_exec($curl); 
	curl_close($curl);
	//echo $result;
	$html = str_get_html($result);
	$num = 0;
	foreach($html->find('#buyCount') as $h){
		$num = $h->innertext;
	}
	$is_end = get_str($html,'Groupon.countdownTimeWhenInit(\'','\')</script>');
	if($is_end==0){
		$db->query("update ".$ecs->table('yhd_group')."set end_time='".time()."' where group_id='".$group_id."'");
	}
	$sql = "insert into ".$ecs->table('yhd_buy')." (group_id,buys_num,add_time) values('".$group_id."','".$num."','".time()."')";
	$db->query($sql);
}

function get_str($str,$start_str,$end_str){
   $start_pos = strpos($str,$start_str)+strlen($start_str);  
   $end_pos = strpos($str,$end_str);  
   $c_str_l = $end_pos - $start_pos;  
   $contents = substr($str,$start_pos,$c_str_l);  
   return $contents;  
} 
?>
